package com.emep.changzhi.analyse.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.cassandra.config.AbstractCassandraConfiguration;
import org.springframework.data.cassandra.config.CassandraClusterFactoryBean;
import org.springframework.data.cassandra.config.SchemaAction;

import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories;

/**
 * @author Wangcc
 * @version V1.0
 * @Description: Cassandra 数据库配置
 * @date 2018/3/5 9:06
 */
@Configuration
@PropertySource(value = { "classpath:cassandra.properties" })
@EnableCassandraRepositories
public class CassandraConfig extends AbstractCassandraConfiguration {

    @Autowired
    private Environment env;

    @Value("${cassandra.keyspace}")
    protected String keySpace;

    @Value("${cassandra.basepackages}")
    private String basePackages;

    //192.168.1.123   tutorialspoint   9042
    @Override
    protected String getKeyspaceName() {
        return keySpace;
    }

    @Override
    public String[] getEntityBasePackages() {
        return new String[] {basePackages};
    }

    @Override
    public SchemaAction getSchemaAction() {
        return SchemaAction.CREATE_IF_NOT_EXISTS;
    }

    public CassandraClusterFactoryBean cluster(){

        CassandraClusterFactoryBean cluster=new CassandraClusterFactoryBean();

        cluster.setContactPoints(env.getProperty("cassandra.contactpoints"));

        cluster.setUsername(env.getProperty("cassandra.username"));

        cluster.setPassword(env.getProperty("cassandra.password"));

        cluster.setPort(new Integer(env.getProperty("cassandra.port")));

        return cluster;
    }
}

